package com.njitzx.controller;


import com.njitzx.entity.po.Category;
import com.njitzx.entity.vo.CategoryVO;
import com.njitzx.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.njitzx.entity.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.Map;

import lombok.RequiredArgsConstructor;



import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Api(tags = "分类表接口")
@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @ApiOperation("商品分类列表")
    @GetMapping
    public Result findPage(@RequestParam(value = "type",required = false)Integer type,@RequestParam(value = "pageNum",required = false) Integer pageNum,@RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> result=categoryService.findPage(type,pageNum,pageSize);
         if (pageNum==null||pageSize==null){
             return Result.success(result.get("list"));
         }else{
             return Result.success(result);
         }
    }

    @ApiOperation("根据主键查询分类表")
    @GetMapping("/{catId}")
    public Result findById(@PathVariable Serializable catId)
    {
        Category cate = categoryService.findById(catId);
        CategoryVO categoryVO=new CategoryVO();
        BeanUtils.copyProperties(cate,categoryVO);
        return Result.success(categoryVO);
    }

    @ApiOperation("新增分类表")
    @PostMapping
    public Result add(@RequestBody Category category){
        //id name  层级
        categoryService.add(category);
        return Result.success();
    }

    @ApiOperation("修改分类表")
    @PutMapping("/{catId}")
    public Result modify(@PathVariable(value = "catId")Integer catId,@RequestBody(required = true)Category category){
        System.out.println(catId);
        category.setCatId(catId);
        categoryService.modify(category);
        return Result.success();
    }

    @ApiOperation("根据主键删除分类表")
    @DeleteMapping("/{catId}")
    public Result removeById(@PathVariable("catId") Integer catId){
        categoryService.removeById(catId);
        return Result.success();
    }

}
